package com.wc.算法提高课.E第五章_数学知识.约数个数.Hankson的趣味题;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.PrintWriter;
import java.util.StringTokenizer;

/**
 * @Author congge
 * @Date 2024/10/4 17:00
 * @description https://www.acwing.com/problem/content/202/
 */
public class Main {
    /**
     * 思路：
     * 已知题意 gcd(x, a) = b, x * c / gcd(x, c) = d
     * 那么可知道 x 一定是 d 的 约数
     * 那么就可以枚举 d 的约数确定答案的数量
     * 如何快速求出 d 的约数呢, 试除法 + 质数筛 + dfs
     */
    static FastReader sc = new FastReader();
    static PrintWriter out = new PrintWriter(System.out);
    static int N = 50010;
    static int[] primes = new int[N];
    static boolean[] st = new boolean[N];
    static int[][] factor = new int[N][2];
    static int[] divider = new int[N];
    // gcd(x, a) = b , (x * c) / gcd(x, c) = d;
    static int a, b, c, d, fcnt, cnt = 0, dcnt;

    public static void main(String[] args) {
        ola();
        int T = sc.nextInt();
        while (T-- > 0) {
            a = sc.nextInt();
            b = sc.nextInt();
            c = sc.nextInt();
            d = sc.nextInt();
            int x = d;
            fcnt = 0;
            for (int i = 0; primes[i] <= x / primes[i]; i++) {
                if (x % primes[i] == 0) {
                    int s = 0;
                    while (x % primes[i] == 0) {
                        x /= primes[i];
                        s++;
                    }
                    factor[fcnt][0] = primes[i];
                    factor[fcnt++][1] = s;
                }
            }
            if (x > 1) {
                factor[fcnt][0] = x;
                factor[fcnt++][1] = 1;
            }
            dcnt = 0;
            dfs(0, 1);
            int res = 0;
            for (int i = 0; i < dcnt; i++) {
                x = divider[i];
                if (gcd(x, a) == b && (long) x * c / gcd(x, c) == d) res++;
            }
            out.println(res);
        }
        out.flush();
    }

    static void dfs(int u, int p) {
        if (u == fcnt) {
            divider[dcnt++] = p;
            return;
        }
        for (int i = 0; i <= factor[u][1]; i++) {
            dfs(u + 1, p);
            p *= factor[u][0];
        }
    }

    static int gcd(int a, int b) {
        return b > 0 ? gcd(b, a % b) : a;
    }

    static void ola() {
        st[0] = st[1] = true;
        for (int i = 2; i < N; i++) {
            if (!st[i]) primes[cnt++] = i;
            for (int j = 0; i * primes[j] < N; j++) {
                st[i * primes[j]] = true;
                if (i % primes[j] == 0) break;
            }
        }
    }
}

class FastReader {
    StringTokenizer st;
    BufferedReader br;

    FastReader() {
        br = new BufferedReader(new InputStreamReader(System.in));
    }

    String next() {
        while (st == null || !st.hasMoreElements()) {
            try {
                st = new StringTokenizer(br.readLine());
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
        return st.nextToken();
    }

    int nextInt() {
        return Integer.parseInt(next());
    }

    String nextLine() {
        String s = "";
        try {
            s = br.readLine();
        } catch (IOException e) {
            e.printStackTrace();
        }
        return s;
    }

    long nextLong() {
        return Long.parseLong(next());
    }

    double nextDouble() {
        return Double.parseDouble(next());
    }

    // 是否由下一个
    boolean hasNext() {
        while (st == null || !st.hasMoreTokens()) {
            try {
                String line = br.readLine();
                if (line == null)
                    return false;
                st = new StringTokenizer(line);
            } catch (IOException e) {
                throw new RuntimeException(e);
            }
        }
        return true;
    }
}
